﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace T.FAS.Runtime.Metadata.Entity.Page
{
    public class PageUIControlJsonConverter : JsonConverter
    {
        public override bool CanRead => true;

        public override bool CanWrite => true;

        public override bool CanConvert(Type objectType)
        {
            return typeof(PageUIControl) == objectType;
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var pageUIControl = new PageUIControl();

            JObject jObj = serializer.Deserialize<JObject>(reader);

            pageUIControl.ID = jObj.Value<string>("ID");
            pageUIControl.CompID = jObj.Value<string>("CompID");
            pageUIControl.CtrlType = jObj.Value<string>("CtrlType");
            pageUIControl.Options = jObj.Value<string>("options");
            pageUIControl.Rules = jObj.Value<string>("Rules");
            pageUIControl.CSS = JsonConvert.DeserializeObject<PageCSS>(Convert.ToString(jObj.Value<JObject>("CSS")));
            pageUIControl.DefaultValue = jObj.Value<string>("DefaultValue");
            pageUIControl.Events = JsonConvert.DeserializeObject<List<PageEvent>>(Convert.ToString(jObj.Value<JArray>("Events")));
            //pageUIControl.MetaDependencys = JsonConvert.DeserializeObject<List<PageUIControlDependency>>(Convert.ToString(jObj.Value<JArray>("MetaDependencys")));
            pageUIControl.InstanceCode = jObj.Value<string>("InstanceCode");
            pageUIControl.Name = jObj.Value<string>("Name");
            pageUIControl.ParentID = jObj.Value<string>("ParentID");
            pageUIControl.Properties = new List<object>();

            IDictionary<string, JToken> dic = jObj;
            if (dic.ContainsKey("children"))
                pageUIControl.ChildrenSerializeName = "children";
            else if (dic.ContainsKey("list"))
                pageUIControl.ChildrenSerializeName = "list";
            else if (dic.ContainsKey("columns"))
                pageUIControl.ChildrenSerializeName = "columns";
            else if (dic.ContainsKey("rows"))
                pageUIControl.ChildrenSerializeName = "rows";
            else if (dic.ContainsKey("tabs"))
                pageUIControl.ChildrenSerializeName = "tabs";
            if (string.IsNullOrEmpty(pageUIControl.ChildrenSerializeName))
                pageUIControl.ChildrenSerializeName = "children";

            pageUIControl.Children = JsonConvert.DeserializeObject<List<PageUIControl>>(Convert.ToString(jObj.Value<JArray>(pageUIControl.ChildrenSerializeName)));
            return pageUIControl;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var data = value as PageUIControl;

            writer.WriteStartObject();
            writer.WritePropertyName("ID"); writer.WriteValue(data.ID);
            writer.WritePropertyName("CompID"); writer.WriteValue(data.CompID);
            writer.WritePropertyName("CtrlType"); writer.WriteValue(data.CtrlType);
            writer.WritePropertyName("options"); writer.WriteValue(data.Options);
            writer.WritePropertyName("Rules"); writer.WriteValue(data.Rules);
            writer.WritePropertyName("CSS"); writer.WriteRawValue(JsonConvert.SerializeObject(data.CSS));
            writer.WritePropertyName("DefaultValue"); writer.WriteValue(data.DefaultValue);
            writer.WritePropertyName("Events"); writer.WriteRawValue(JsonConvert.SerializeObject(data.Events));
            //writer.WritePropertyName("MetaDependencys"); writer.WriteRawValue(JsonConvert.SerializeObject(data.MetaDependencys));
            writer.WritePropertyName("InstanceCode"); writer.WriteValue(data.InstanceCode);
            writer.WritePropertyName("Name"); writer.WriteValue(data.Name);
            writer.WritePropertyName("ParentID"); writer.WriteValue(data.ParentID);
            writer.WritePropertyName("Properties"); writer.WriteRawValue("[]");

            if (string.IsNullOrEmpty(data.ChildrenSerializeName))
                data.ChildrenSerializeName = "children";

            writer.WritePropertyName("ChildrenSerializeName"); writer.WriteValue(data.ChildrenSerializeName);
            writer.WritePropertyName(data.ChildrenSerializeName); writer.WriteRawValue(JsonConvert.SerializeObject(data.Children));

            writer.WriteEndObject();
        }
    }
}
